package com.spring.sunflower.common

import android.view.View
import android.widget.LinearLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.listener.OnRefreshListener
import com.spring.sunflower.R
import com.spring.sunflower.base.BaseFragment
import com.spring.sunflower.bean.TaskAwardDailyBean
import com.spring.sunflower.bean.TaskAwardSignBean

/**
 * Created by Kevin on 2021/8/27<br/>
 *
 * Describe:<br/>
 */
class TaskNewerFragment : BaseFragment<AwardPresenter>(), CommonContract.AwardView,
    OnLoadMoreListener, OnRefreshListener {
    private var refreshLayout: SmartRefreshLayout? = null
    private var rvRecyclerView: RecyclerView? = null
    private var mAdapter: AwardDailyAdapter? = null
    private var mData: MutableList<TaskAwardDailyBean.DatasBean> = ArrayList()
    private var pageIndex = 1
    private var llEmpty: LinearLayout? = null
    override fun setLayoutResId(): Int = R.layout.fragment_sub_award

    override fun initView() {
        llEmpty = mView.findViewById(R.id.llEmpty)
        refreshLayout = mView.findViewById(R.id.refreshLayout)
        rvRecyclerView = mView.findViewById(R.id.rvRecyclerView)
        mAdapter = AwardDailyAdapter(R.layout.adapter_item_award, mData)
        val linearLayoutManager = LinearLayoutManager(mActivity)
        rvRecyclerView?.let {
            it.layoutManager = linearLayoutManager
            it.adapter = mAdapter
        }
        refreshLayout!!.setOnRefreshListener(this)
        refreshLayout!!.setOnLoadMoreListener(this)

    }

    override fun initData() {
        mPresenter.getNewerAward(pageIndex.toString())
    }

    override fun initListener() {
    }

    override fun loadData() {
    }

    override fun setPresenter(): AwardPresenter = AwardPresenter(this)
    override fun showDailyAward(bean: MutableList<TaskAwardDailyBean.DatasBean>?) {
    }

    override fun showNewerAward(bean: MutableList<TaskAwardDailyBean.DatasBean>?) {
        if (pageIndex == 1) {
            if (bean.isNullOrEmpty()) {
                llEmpty!!.visibility = View.VISIBLE
                refreshLayout!!.visibility = View.GONE
            } else {
                llEmpty!!.visibility = View.GONE
                refreshLayout!!.visibility = View.VISIBLE
                mAdapter!!.setList(bean)
            }
        } else {
            mAdapter!!.addData(bean!!)
        }
        if (refreshLayout!!.isLoading) {
            refreshLayout!!.finishLoadMore()
        }
        if (refreshLayout!!.isRefreshing) {
            refreshLayout!!.finishRefresh()
        }

    }

    override fun showSignAward(bean: MutableList<TaskAwardSignBean.DatasBean>?) {
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        mPresenter.getNewerAward((++pageIndex).toString())
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        pageIndex = 1
        mPresenter.getNewerAward(pageIndex.toString())
    }
}